<?php
/**
 * Created by PhpStorm.
 * User: Duke
 * Date: 2019/12/2
 * Time: 15:45
 */
namespace App\Http\Controllers\Admin;

use App\Exceptions\CoustomerException;
use App\Http\Logic\admin\ArticleLogic;
use App\Model\AdminMenuModel;

class SettingController extends AlphaController{

    public function __construct(){

    }

    //首页
    public function index(){
        return view('admin/article/index');
    }
    //获取列表信息
    public function getInfo(){
        try{
            $article = new ArticleLogic();
            $list = $article->articleAllList();
            $ret = ['code'=>0,'msg'=>'成功','data'=>$list];
        }catch (CoustomerException $e){
            $ret = ['code'=>$e->getCode(),'msg'=>$e->getMessage()];
        }
        return $ret;
    }
    //添加
    public function add(){
        $admin_category = new ArticleLogic();
        $category = $admin_category->getCategoryList('0');
        return view('admin/index/add',[ 'category' =>  $category]);
    }
    //编辑
    public function edit(){
        $category = new AdminMenuModel();
        $admin_category = new ArticleLogic();
        $categoryInfo = $category->find_info();
        $category = $admin_category->getCategoryList('0');
        return view('admin/index/edit',['ca' => $categoryInfo, 'category' =>  $category]);
    }
    //保存 & 添加
    public function save(){
        $category = new AdminMenuModel();
        if($_POST['type'] == "1"){
            $i = json_decode($_POST['data'],true);
            $post = [
                'pid' => $i['pid'],
                'name' => $i['name'],
            ];
            $categoryInfo = $category->insert_info($post);
        }else{
            $post = json_decode($_POST['data'],true);
            $post = [
                'pid' => $post['pid'],
                'id' => $post['id'],
                'name' => $post['name'],
            ];
            $categoryInfo = $category->update_info($post);
        }
        $ret = ['code'=>0,'msg'=>'成功','data'=>$categoryInfo];
        return json_encode($ret);
    }
    //删除
    public function del(){

    }
}
